#!/bin/bash

#
# Prepares global available variables usually defined in /etc/webdevappliance/vhost-admin.conf
#
prepare_vars() {
        REMAINING_DOMAIN=`pv "$REMAINING_DOMAIN" "#USER#" "$USER" "#GROUP#" "$GROUP"`
        DOMAIN=`pv "$DOMAIN" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}" "#VHOST#" "${VHOST}"`
	USER_HOME_PATH=`pv "$USER_HOME_PATH" "#USER#" "${USER}" "#GROUP#" "${GROUP}"`
	USER_BIN_PATH=`pv "$USER_BIN_PATH" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#USER_HOME_PATH#" "${USER_HOME_PATH}"`
        VHOSTS_DIR=`pv "$VHOSTS_DIR" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}"	"#VHOST#" "${VHOST}" "#USER_HOME_PATH#" "${USER_HOME_PATH}" "#DOMAIN#" "${DOMAIN}"`
	VHOST_DIR=`pv "$VHOST_DIR" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}" "#VHOST#" "${VHOST}" "#USER_HOME_PATH#" "${USER_HOME_PATH}" "#DOMAIN#" "${DOMAIN}" "#VHOSTS_DIR#" "${VHOSTS_DIR}"`
	VHOST_PROFILE_PATH=`pv "$VHOST_PROFILE_PATH" "#VHOST_PROFILES_PATH#" "${WDA_VHOST_PROFILES_PATH}" "#VHOST_PROFILE#" "${VHOST_PROFILE}"`
	VHOST_PROFILE_SKELFILE_PATH=`pv "${VHOST_PROFILE_SKELFILE_PATH}"  "#VHOST_PROFILE_PATH#" "$VHOST_PROFILE_PATH"`
	VHOST_PROFILE_SKEL_PATH=`pv "$VHOST_PROFILE_SKEL_PATH" "#VHOST_PROFILE_PATH#" "$VHOST_PROFILE_PATH"` 
	return 0
}

# VARNAME=`pv STRING-TEXT search1 replace1 [search2 replace2 search3 replace3 searchN replaceN]`
# Short for 'prepare Variable' or 'parse Variable'
pv() {
	local STRING=$1
	shift
	until [[ -z "$1" && -z "$2" ]]
	do
		if [[ ! -z "$1" && ! -z "$2" ]] ; then
			STRING=${STRING//"$1"/"$2"}
		fi
		shift; shift;
	done
	echo $STRING
	return 0
}

# pf PATH_TO_FILE search1 replace1 [search2 replace2 search3 replace3] > PATH_TO_NEW_FILE
# Used to parse a file and replace all given search string with the given replace strings.
# The result is sent to stdout. You can add Option -i which 'interactively' does the changes in the file.
pf() {
	local SEDOPTS=""
	local OPTION

	# This is important, when calling this function form another function
	# which uses getopts.
	local OPTIND=1
	local FILE

        while getopts 'i' OPTION
	do
                case $OPTION in 
                        i) 
                                SEDOPTS="-i"
                        ;; 
                esac
        done

	shift $(( OPTIND -1 ))

	FILE=$1
	shift

	if [[ ! -f "$FILE" ]] ; then
		return 1
	fi
	local SEDSCRIPT=""
	until [[ -z "$1" && -z "$2" ]]
        do
		if [[ ! -z "$1" && ! -z "$2" ]] ; then
                	SEDSCRIPT="${SEDSCRIPT}s$1$2g
			"
		fi
                shift; shift;
        done
	sed $SEDOPTS "$SEDSCRIPT" "$FILE"
	return 0
}


# pmf -f "path/to/file1 /path/to/file2 ...." "#FOO#" "$FOO" "#BAR#" "$BAR"
# search and replaces the same patterns in the given files at the same time.
# Make sure to seperate the files with a whitespace and put all files in quotes.
pmf() {
	local OPTION
	local OPTIND=1
	local FILES
	local FILE

	while getopts 'f:' OPTION
	do
		case $OPTION in
			f)
				FILES=$OPTARG
			;;
		esac
	done

	# Remove used Arguments (-f)
	shift $(( OPTIND -1 ))

	if [[ -z $FILES ]] ; then
		return 1
	fi

	for FILE in $FILES
	do
		pf -i "$FILE" "$@"
		if [[ $? -gt 0 ]] ; then
			return $?
		fi
	done
	return 0
}



base_apache_setup() {
	echo "Apache setup"
	echo 
	echo "pf:"
        pf -i "$L_VHOSTCONFFILE_PATH" "#VHOST#" "${VHOST}" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}" "#VHOSTS_DIR#" "${VHOSTS_DIR}" "#VHOST_DIR#" "${VHOST_DIR}" "#DOMAIN#" "${DOMAIN}" "#VHOST_CFG_PATH#" "$CFG_PATH" "#VHOST_BIN_PATH#" "$BIN_PATH" "#VHOST_WWW_PATH#" "$WWW_PATH" "#VHOST_LOG_PATH#" "$LOG_PATH" "#VHOST_PUBLIC_PATH#" "$PUBLIC_PATH" "#VHOST_FCGIFILE_PATH#" "$FCGIFILE_PATH" "#VHOST_PHPRC_PATH#" "$PHPRC_PATH" "#VHOST_PHPINIFILE_PATH#" "$PHPINIFILE_PATH" "#VHOST_CONFIGFILE_PATH#" "$VHOSTCONFFILE_PATH"
	return $?
}



base_fcgid_setup() {
        pf -i "$L_FCGIFILE_PATH" "#VHOST#" "${VHOST}" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}" "#VHOSTS_DIR#" "${VHOSTS_DIR}" "#VHOST_DIR#" "${VHOST_DIR}" "#DOMAIN#" "${DOMAIN}" "#VHOST_CFG_PATH#" "$CFG_PATH" "#VHOST_BIN_PATH#" "$BIN_PATH" "#VHOST_WWW_PATH#" "$WWW_PATH" "#VHOST_LOG_PATH#" "$LOG_PATH" "#VHOST_PUBLIC_PATH#" "$PUBLIC_PATH" "#VHOST_FCGIFILE_PATH#" "$FCGIFILE_PATH" "#VHOST_PHPRC_PATH#" "$PHPRC_PATH" "#VHOST_PHPINIFILE_PATH#" "$PHPINIFILE_PATH" "#VHOST_CONFIGFILE_PATH#" "$VHOSTCONFFILE_PATH"
	return $?
}



base_php_setup() {
        # Remove all Comments
        sed -i "/^;/d" $L_PHPINIFILE_PATH
	if [[ $? -gt 0 ]] ; then
		return $?
	fi

	# Remove all empty lines
	sed -i "/^$/d" $L_PHPINIFILE_PATH
	if [[ $? -gt 0 ]] ; then
		return $?
	fi

        # Insert some Settings after [PHP] which are commented by default. Only to be sure sed finds this setting on the next line and can substitute this settings.
        sed -i "/\[PHP\]/{n;s/.*/error_log = \\ncgi.fix_pathinfo = \\nopen_basedir = \\nerror_reporting = \\nlog_errors = \\ndoc_root = \\ninclude_path = /;}" $L_PHPINIFILE_PATH
	if [[ $? -gt 0 ]] ; then
		return $?
	fi

	# Substitute original with our modified settings.
	sed -i "/^error_reporting[ ]*=.*/s.*error_reporting=E_ALL \& E_STRICTg
	/^log_errors[ ]*=.*/s.*log_errors=Ong
	/^error_log[ ]*=.*/s.*error_log=${LOG_PATH}/php-error.logg
	/^doc_root[ ]*=.*/s.*doc_root=$PUBLIC_PATHg
	/^cgi.fix_pathinfo[ ]*=.*/s.*cgi.fix_pathinfo=1g
	/^open_basedir[ ]*=.*/s.*open_basedir=${VHOST_DIR}g
	/^include_path[ ]*=.*/s.*include_path=\".:/usr/share/php5:usr/share/php\"g" $L_PHPINIFILE_PATH
	if [[ $? -gt 0 ]] ; then
		return $?
	fi

        # Delete all duplicated lines in the file.
        sed -ni 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' $L_PHPINIFILE_PATH
	if [[ $? -gt 0 ]] ; then
		return $?
	fi

        pf -i "$L_PHPINIFILE_PATH" "#VHOST#" "${VHOST}" "#USER#" "${USER}" "#GROUP#" "${GROUP}" "#REMAINING_DOMAIN#" "${REMAINING_DOMAIN}" "#VHOSTS_DIR#" "${VHOSTS_DIR}" "#VHOST_DIR#" "${VHOST_DIR}" "#DOMAIN#" "${DOMAIN}" "#VHOST_CFG_PATH#" "$CFG_PATH" "#VHOST_BIN_PATH#" "$BIN_PATH" "#VHOST_WWW_PATH#" "$WWW_PATH" "#VHOST_LOG_PATH#" "$LOG_PATH" "#VHOST_PUBLIC_PATH#" "$PUBLIC_PATH" "#VHOST_FCGIFILE_PATH#" "$FCGIFILE_PATH" "#VHOST_PHPRC_PATH#" "$PHPRC_PATH" "#VHOST_PHPINIFILE_PATH#" "$PHPINIFILE_PATH" "#VHOST_CONFIGFILE_PATH#" "$VHOSTCONFFILE_PATH"
	return $?
}

